<?php
##############################################
#
#   菜单配置
# @ Time：2016-12-9 - 上午12:11:44
# @ Author ：JiangYongWei <1085562810@qq.com>
# @ Encoding ：UTF-8 language ：PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\MenuModel;
use Admin\Model\AdminBaseModel;

class MenuController extends AdminBaseController {
	
	protected $_menuObject;
	
	public function _initialize(){
		parent::_initialize();
		$this -> _menuObject  = new MenuModel();
	}
	
	/**
	 * 展示菜单
	 */
    public function menuList(){
//     	$data=$this -> _menuObject -> getTreeData('tree','order_number,id');
		$model = new AdminBaseModel('admin_nav');
		$data = $model -> getTreeData('tree','order_number,id');
		//var_dump($data);
    	$this -> assign('data',$data);
        $this -> display('menu_list');
    }
    
    /**
     * 添加菜单
     */
   	public function addMenu(){
   		if(!IS_POST) return;
   		$data['p_id'] = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;
   		if(!$data['p_id']){
   		   $order_number = M('admin_nav')->max('order_number');
   		   $data['order_number'] = $order_number + 1;
   		}else{
   		   $data['order_number'] = 'default';
   		}
   		$data['name'] = trim(I('post.menu_name'));
   		$data['mca'] = trim(I('post.link'));
   		$data['ico'] = trim(I('post.ico'));
   		$result = $this -> _menuObject -> addOneLevelMenu($data);
   		if($result)
   			$this -> success('添加成功', U('menuList'));
   		else
   			$this -> error('操作失败', U('menuList'));
   	}
   	
   	/**
   	 * 删除菜单
   	 */
   	public function deleteMenu(){
   		if(!IS_AJAX) return;
   		$result = $this -> _menuObject -> deleteMenu(intval($_GET['id']));
   		if($result == 1)
   			$this -> ajaxReturn(array('status' => 1,'message' => '该菜单有子菜单，不可删除'));
   		elseif($result == 2)
   			$this -> ajaxReturn(array('status' => 2,'message' => '删除成功'));
   		elseif($result == 3)
   			$this -> ajaxReturn(array('status' => 3,'message' => '操作失败'));
   	}
   	
   	/**
   	 * 修改菜单
   	 */
   	public function editMenu(){
   		$data['id'] = intval(I('get.id'));
   		$data['name'] = trim(I('post.menu_name'));
   		$data['mca'] = trim(I('post.link'));
   		$data['ico'] = trim(I('post.ico'));
   		$result = $this -> _menuObject -> editMenu($data);
   		if($result)
   			$this -> success('修改成功', U('menuList'));
   		else
   			$this -> error('操作失败', U('menuList'));
   	}
   	
   	public function updateSort(){
   		$data = I("post.");
   		$result = updateSort('admin_nav',$data);
   		if($result)
   			$this -> success('排序成功',U('menuList'),1);
   		else
   			$this -> error('操作失败');
   	}

    /**
     *状态展示
     * @Date 201707
     * @Author Lin
     */
    public function statusEdit()
    {
        $menuId = I('menuId');
        $status = I('status');
        //var_dump($menuId);var_dump($status);die;
        $childMenu = M('admin_nav') -> where(array('pid'=>$menuId)) -> select();

        if($childMenu && $status == 0)
        {
            foreach ($childMenu as $k => $v)
            {
                M('admin_nav')->where(array('id'=>$v['id']))->setField('is_display',$status);
            }
            $result = M('admin_nav')->where(array('id'=>$menuId))->setField('is_display',$status);
        }
        else
        {
          $result = M('admin_nav')->where(array('id'=>$menuId))->setField('is_display',$status);
        }

        //$result = M('admin_nav')->where(array('id'=>$menuId))->setField('is_display',$status);

        if ($result !== false)
            $this->ajaxReturn(array('status'=>1));
        else
            $this->ajaxReturn(array('status'=>0));
    }
}